package com.leyou.mall.controller;


import com.leyou.common.pojo.R;
import com.leyou.mall.data.dto.MallSpecsDto;
import com.leyou.mall.mapper.MallSpecsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xhh
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/leyou.mall/mall-specs")
public class MallSpecsController {
    @Autowired
    private MallSpecsMapper mallSpecsMapper;
    /**
    * todo 手机端获取规格
    * @param id
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/15 14:41
    * @deprecated 
    */
    @RequestMapping(value = "/getListByGoodsId",method = RequestMethod.POST)
    public R getListByGoodsId(@RequestParam("goodsId")Integer id){
        //LambdaQueryWrapper<MallSpecs> queryWrapper=new LambdaQueryWrapper();
        //queryWrapper.eq(MallSpecs::getGoodsId,id);
        //List<MallSpecs> list = mallSpecsService.list(queryWrapper);
        List<MallSpecsDto> goodsSpecsByGoodsId = mallSpecsMapper.getGoodsSpecsByGoodsId(id);

        Map<String, HashSet<String>> map=new HashMap<>(16);
        for (MallSpecsDto mallSpecsDto : goodsSpecsByGoodsId) {
            if(map.get(mallSpecsDto.getOneSpecs())==null){
                map.put(mallSpecsDto.getOneSpecs(),new HashSet<>());
            }
            if(map.get(mallSpecsDto.getTwoSpecs())==null){
                if(mallSpecsDto.getTwoSpecs()!=null && !mallSpecsDto.getTwoSpecs().equals("")) {
                    map.put(mallSpecsDto.getTwoSpecs(),new HashSet<>());
                }
            }
            if(map.get(mallSpecsDto.getThreeSpecs())==null){
                if(mallSpecsDto.getThreeSpecs()!=null && !mallSpecsDto.getThreeSpecs().equals("")) {
                    map.put(mallSpecsDto.getThreeSpecs(), new HashSet<>());
                }
            }
            HashSet<String> hashSet1 = map.get(mallSpecsDto.getOneSpecs());
            HashSet<String> hashSet2 = map.get(mallSpecsDto.getTwoSpecs());
            HashSet<String> hashSet3 = map.get(mallSpecsDto.getThreeSpecs());

            hashSet1.add(mallSpecsDto.getOneStyle());
            map.put(mallSpecsDto.getOneSpecs(),hashSet1);
            if(map.get(mallSpecsDto.getTwoSpecs())!=null) {
                hashSet2.add(mallSpecsDto.getTwoStyle());
                map.put(mallSpecsDto.getTwoSpecs(), hashSet2);
            }
            if(map.get(mallSpecsDto.getThreeSpecs())!=null) {
                hashSet3.add(mallSpecsDto.getThreeStyle());
                map.put(mallSpecsDto.getThreeSpecs(), hashSet3);
            }
        }
        map.entrySet().forEach(System.out::println);
        return R.ok().data("list",goodsSpecsByGoodsId).data("specsList",map);
    }

}

